﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class admin_artcles_tag : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        scriptmsg.Text="";
        if (!IsPostBack)
        {
            LoadTagList();
        }
    }

    protected void LoadTagList()
    {
        DataSet ds = new DataSet();
        ds = new Cstudy.BLL.postTag().SelectPostTag();
        TagList.DataSource = ds;
        TagList.DataBind();
    }

    protected void Unnamed1_SelectedIndexChanged(object sender, EventArgs e)
    {
        NameBox.Text = TagList.SelectedRow.Cells[2].Text;
        RemarkBox.Text = TagList.SelectedRow.Cells[3].Text;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TagList.SelectedIndex < 0)
        {
            scriptmsg.Text = "<script>alert('未选择修改项!')</script>";
        }
        else if (NameBox.Text.Trim() == "")
        {
            scriptmsg.Text = "<script>alert('数据填写不完整!')</script>";
        }
        else
        {
            Cstudy.MODEL.postTag postTag = new Cstudy.MODEL.postTag();
            postTag.TagID = int.Parse(TagList.SelectedRow.Cells[1].Text);
            postTag.TagName = NameBox.Text;
            postTag.TagRemarks = RemarkBox.Text;
            if (new Cstudy.BLL.postTag().UpdatePostTag(postTag))
            {
                scriptmsg.Text = "<script>alert('修改成功!');window.location.href='tag.aspx';</script>";
            }
            else
            {
                scriptmsg.Text = "<script>alert('修改失败!')</script>";
            }
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (NameBox.Text.Trim() == "")
        {
            scriptmsg.Text = "<script>alert('数据填写不完整!')</script>";
        }
        else
        {
            Cstudy.MODEL.postTag postTag = new Cstudy.MODEL.postTag();
            postTag.TagName = NameBox.Text;
            postTag.TagRemarks = RemarkBox.Text;
            if (new Cstudy.BLL.postTag().AddPostTag(postTag))
            {
                scriptmsg.Text = "<script>alert('添加成功!');window.location.href='tag.aspx';</script>";
            }
            else
            {
                scriptmsg.Text = "<script>alert('添加失败!')</script>";
            }
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        try
        {
            int id = int.Parse(TagList.SelectedRow.Cells[1].Text);
            if (new Cstudy.BLL.postTag().DeletePostTag(id))
            {
                scriptmsg.Text = "<script>alert('删除成功!');window.location.href='tag.aspx';</script>";
            }
            else
            {
                scriptmsg.Text = "<script>alert('删除失败!')</script>";
            }
        }
        catch (NullReferenceException ex)
        {
            scriptmsg.Text = "<script>alert('删除失败!没有选择删除项！')</script>";
        }
    }
}